const app = getApp();
Page({
  data: {
    hidden: true,
    myclass:'',
    dataList :[
      // [
      //  {name:'安测试', phone: '15949646360',  avatar: 'https://7465-test-0gnibtq0d1e1243a-1304764432.tcb.qcloud.la/avatar.png?sign=5955b4c34084e967ae5196792fef2c55&t=1618469622', studentId: "8002117151" },
      //  {name:'安静', phone: '15949646361',  avatar: 'https://7465-test-0gnibtq0d1e1243a-1304764432.tcb.qcloud.la/avatar.png?sign=5955b4c34084e967ae5196792fef2c55&t=1618469622', studentId: "8002117151" },
      //  {name:'艾测试', phone: '15949646362',  avatar: 'https://7465-test-0gnibtq0d1e1243a-1304764432.tcb.qcloud.la/avatar.png?sign=5955b4c34084e967ae5196792fef2c55&t=1618469622', studentId: "8002117151" },
      //  {name:'艾青', phone: '15949646363',  avatar: 'https://7465-test-0gnibtq0d1e1243a-1304764432.tcb.qcloud.la/avatar.png?sign=5955b4c34084e967ae5196792fef2c55&t=1618469622', studentId: "8002117151" }
      // ],[
      //   {name:'贝多芬', phone: '15949646364',  avatar: 'https://7465-test-0gnibtq0d1e1243a-1304764432.tcb.qcloud.la/avatar.png?sign=5955b4c34084e967ae5196792fef2c55&t=1618469622', studentId: "8002117151" },
      //   {name:'邶立', phone: '15949646365',  avatar: 'https://7465-test-0gnibtq0d1e1243a-1304764432.tcb.qcloud.la/avatar.png?sign=5955b4c34084e967ae5196792fef2c55&t=1618469622', studentId: "8002117151" }
      // ], [
      //   {name:'蔡司', phone: '15949646360',  avatar: 'https://7465-test-0gnibtq0d1e1243a-1304764432.tcb.qcloud.la/avatar.png?sign=5955b4c34084e967ae5196792fef2c55&t=1618469622', studentId: "8002117151" },
      //  {name:'蔡家佩', phone: '15949646360',  avatar: 'https://7465-test-0gnibtq0d1e1243a-1304764432.tcb.qcloud.la/avatar.png?sign=5955b4c34084e967ae5196792fef2c55&t=1618469622', studentId: "8002117151" },
      //  {name:'蔡静', phone: '15949646360',  avatar: 'https://7465-test-0gnibtq0d1e1243a-1304764432.tcb.qcloud.la/avatar.png?sign=5955b4c34084e967ae5196792fef2c55&t=1618469622', studentId: "8002117151" },
      //  {name:'蔡进', phone: '15949646360',  avatar: 'https://7465-test-0gnibtq0d1e1243a-1304764432.tcb.qcloud.la/avatar.png?sign=5955b4c34084e967ae5196792fef2c55&t=1618469622', studentId: "8002117151" }
      // ]
    ]
  },
  onLoad() {

    let list = [];
    for (let i = 0; i < 26; i++) {
      list[i] = String.fromCharCode(65 + i)
    }
    this.setData({
      list: list, // [A, B, C, D, E]
      listCur: list[0]   // A 
    })

    let userInfo  = wx.getStorageSync('userInfo')

    if(userInfo == "" ){
      wx.showToast({
        title: '未登录',
        icon: 'error',
        duration: 2000
      })
    }else{
      let myclass = userInfo.class
      this.setData({
        myclass
      })
      wx.cloud.callFunction({
        // 需调用的云函数名
        name: 'getStudentContact',
        // 传给云函数的参数
        data: {
          myclass
        }
      }).then(res =>{
        // console.log(res.result.data)
        let userList = res.result.data
        var resArray = Object.values(userList.reduce((res, item) => {
          res[item.block] ? res[item.block].push(item) : res[item.block] = [item];
          for (let i = 0; i < 26; i++) {
              if (!res[i]) {
                  res[i] = []
              }
          }
          return res;
      }, {}));
      // console.log(resArray)
      this.setData({
        dataList : resArray
      })
      })
    }

   
  },
  onReady() {
    let that = this;
    wx.createSelectorQuery().select('.indexBar-box').boundingClientRect(function(res) {
      that.setData({
        boxTop: res.top
      })
    }).exec();
    wx.createSelectorQuery().select('.indexes').boundingClientRect(function(res) {
      that.setData({
        barTop: res.top
      })
    }).exec()
  },
  //获取文字信息
  getCur(e) {
    this.setData({
      hidden: false,
      listCur: this.data.list[e.target.id],
    })
  },

  setCur(e) {
    this.setData({
      hidden: true,
      listCur: this.data.listCur
    })
  },
  //滑动选择Item
  tMove(e) {
    let y = e.touches[0].clientY,
      offsettop = this.data.boxTop,
      that = this;
    //判断选择区域,只有在选择区才会生效
    if (y > offsettop) {
      let num = parseInt((y - offsettop) / 20);
      this.setData({
        listCur: that.data.list[num]
      })
    };
  },

  //触发全部开始选择
  tStart() {
    this.setData({
      hidden: false //  刚触摸时 显示选择
    })
  },

  //触发结束选择
  tEnd() {
    this.setData({
      hidden: true,  //  触摸结束时  隐藏选择
      listCurID: this.data.listCur   // 
    })

    // console.log(this.data.listCur)  //  A\B\C 
  },
  indexSelect(e) {
    let that = this;
    let barHeight = this.data.barHeight;
    let list = this.data.list;
    let scrollY = Math.ceil(list.length * e.detail.y / barHeight);
    for (let i = 0; i < list.length; i++) {
      if (scrollY < i + 1) {
        that.setData({
          listCur: list[i],
          movableY: i * 20
        })
        return false
      }
    }
  },


  tofuzzyQuery(){
    wx.navigateTo({
      url: './fuzzyQuery?myclass='+ this.data.myclass,
    })
  },

  // 拨打电话
  callPhone(e){
    // console.log(e)
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone, 
    })
  },

  onShow(){
    let userInfo = wx.getStorageSync('userInfo')
  }
});